//
//  NSString+Time.m
//  basic
//
//  Created by Jack on 2016/12/13.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "NSString+Time.h"
#import "NSString+extend.h"

@implementation NSString (Time)

static NSMutableDictionary *dateFormatters = nil;

+ (NSString *) getTimeDate:(NSDate *)time withFormat:(NSString *)format
{
    if (!time) {
        return @"";
    }
    
    if (![NSString isNotEmpty:format]) {
        format = @"yyyy-MM-dd";
    }
    
    if (dateFormatters == nil) {
        dateFormatters = [[NSMutableDictionary alloc] init];
    }
    
    if (![dateFormatters.allKeys containsObject:format]) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:format];
        [dateFormatters setValue:dateFormatter forKey:format];
        return [dateFormatter stringFromDate:time];
    }
    else {
        NSDateFormatter *dateFormatter = dateFormatters[format];
        return [dateFormatter stringFromDate:time];
    }
    
    return nil;
}

+ (NSString *) getaTimeString:(NSString *)timeString{
    
    if (!timeString) {
        return @"";
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    if ([timeString isEqualToStr:@"0"]) {
        return @"";
    }
    
    if (timeString.length < 10) {
        return timeString;
    }
    
    timeString = [timeString substringWithRange:NSMakeRange(0, 10)];
    
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue];
    // Calculate distance time string
    //    time = [NSString getBeijingTime:time];
    
    static NSDateFormatter *dateFormatter = nil;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy/MM/dd"];
    }
    
    return [dateFormatter stringFromDate:time];
}


+ (NSString *) getTimeString:(NSString *)timeString{
    
    if (!timeString) {
        return @"";
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    if ([timeString isEqualToStr:@"0"]) {
        return @"";
    }
    
    if (timeString.length < 10) {
        return timeString;
    }
    
    timeString = [timeString substringWithRange:NSMakeRange(0, 10)];
    
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue];
    // Calculate distance time string
//    time = [NSString getBeijingTime:time];
    
    static NSDateFormatter *dateFormatter = nil;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    
    return [dateFormatter stringFromDate:time];
}

+ (NSString *) getShortTimeString:(NSString *)timeString{
    
    if (!timeString) {
        return @"";
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    if ([timeString isEqualToStr:@"0"]) {
        return @"";
    }
    
    if (timeString.length < 10) {
        return timeString;
    }
    
    timeString = [timeString substringWithRange:NSMakeRange(0, 10)];
    
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue];
    // Calculate distance time string

//    time = [NSString getBeijingTime:time];
    
    static NSDateFormatter *dateFormatter = nil;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM-dd"];
    }
    
    return [dateFormatter stringFromDate:time];
}

+ (NSString *) getTime2String:(NSString *)timeString{
    
    if (!timeString) {
        return @"";
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    if ([timeString isEqualToStr:@"0"]) {
        return @"";
    }
    
    if (timeString.length < 10) {
        return timeString;
    }
    
    timeString = [timeString substringWithRange:NSMakeRange(0, 10)];
    
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue];
    // Calculate distance time string

//    time = [NSString getBeijingTime:time];
    
    static NSDateFormatter *dateFormatter = nil;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }
    
    return [dateFormatter stringFromDate:time];
}

+ (NSString *) getTime3String:(NSString *)timeString{
    
    if (![NSString isNotEmpty:timeString]) {
        return @"";
    }
    
    if ([timeString isEqualToStr:@"0"]) {
        return @"";
    }
    
    if (timeString.length < 10) {
        return timeString;
    }
    
    timeString = [timeString substringWithRange:NSMakeRange(0, 10)];
    
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue];
    // Calculate distance time string
    
//    time = [NSString getBeijingTime:time];
    
    static NSDateFormatter *dateFormatter = nil;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    }
    
    return [dateFormatter stringFromDate:time];
}


+ (NSString *) getTime4String:(NSString *)timeString{
    
    if (!timeString) {
        return @"";
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    

    if ([timeString isEqualToStr:@"0"]) {
        return @"";
    }
    
    if (timeString.length < 10) {
        return timeString;
    }
    
    timeString = [timeString substringWithRange:NSMakeRange(0, 10)];
    
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue];
    // Calculate distance time string
    
    //    time = [NSString getBeijingTime:time];
    
    static NSDateFormatter *dateFormatter = nil;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    }
    
    return [dateFormatter stringFromDate:time];
}


+ (NSString *) getTime5String:(NSString *)timeString{
    
    if (!timeString) {
        return @"";
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    
    if ([timeString isEqualToStr:@"0"]) {
        return @"";
    }
    
    if (timeString.length < 10) {
        return timeString;
    }
    
    timeString = [timeString substringWithRange:NSMakeRange(0, 10)];
    
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue];
    // Calculate distance time string
    
    //    time = [NSString getBeijingTime:time];
    
    static NSDateFormatter *dateFormatter = nil;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
    }
    
    return [dateFormatter stringFromDate:time];
}



+ (NSString *) getBBYTimeString:(NSString *)timeString
{
    if (!timeString) {
        return @"";
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    if (timeString.length < 10) {
        return timeString;
    }

    
    static NSDateFormatter *originalDateFormatter;
    static NSDateFormatter *originalDateFormatter2;
    if (originalDateFormatter == nil) {
        originalDateFormatter = [[NSDateFormatter alloc] init];
        [originalDateFormatter setDateFormat:@"yyyyMMddHHmm"];
    }
    
    if (originalDateFormatter2 == nil) {
        originalDateFormatter2 = [[NSDateFormatter alloc] init];
        [originalDateFormatter2 setDateFormat:@"yyyyMMddHHmmss"];
    }
    
    NSDate *time = [originalDateFormatter dateFromString:timeString];
    if (!time) {
        time = [originalDateFormatter2 dateFromString:timeString];
    }
    
//    time = [NSString getBeijingTime:time];
    
    static NSDateFormatter *dateFormatter;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM月dd日HH:mm"];
    }
    
    NSString *string  = [dateFormatter stringFromDate:time];
    
    //    }
    return string;
}

//+ (NSString *) getBBYTNowTimeString
//{
//    static NSDateFormatter *dateFormatter = nil;
//
//    if (dateFormatter == nil) {
//        dateFormatter = [[NSDateFormatter alloc] init];
//        [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
//    }
//    
//    NSString *string = [dateFormatter stringFromDate:[NSDate date]];
//    
//    return string;
//}


+ (NSString *)getXFTimeString:(NSString *)timeString
{
    if (!timeString) {
        return @"";
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    if (timeString.length < 10) {
        return timeString;
    }
    
//    if (![NSString isTimestamp:timeString]) {
//        return timeString;
//    }
    timeString = [timeString substringWithRange:NSMakeRange(0, 10)];
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue];

//    time = [NSString getBeijingTime:time];
    
    int distance = [[NSDate date] timeIntervalSinceDate:time];
    NSString *string = nil;
    if (distance == 0){//avoid 0 seconds
        string = @"刚刚";
    }
    else if (distance > 0 && distance < 60) {
        string = [NSString stringWithFormat:@"%d秒前", (distance)];
    }
    else if (distance > 0 && distance < 3600) {//60 * 60
        distance = distance / 60;
        string = [NSString stringWithFormat:@"%d分钟前", (distance)];
    }
    else if (distance > 0 && distance < 86400) {//60 * 60 * 24
        distance = distance / 3600;
        string = [NSString stringWithFormat:@"%d小时前", (distance)];
    }
//    else if (distance > 0 && distance < 604800) {//60 * 60 * 24 * 7
//        distance = distance / 86400;
//        string = [NSString stringWithFormat:@"%d天前", (distance)];
//    }
//    else if (distance > 0 && distance < 2419200) {//60 * 60 * 24 * 7 * 4
//        distance = distance / 604800;
//        string = [NSString stringWithFormat:@"%d周前", (distance)];
//    }
    else {
        static NSDateFormatter *dateFormatter = nil;
        
        if (dateFormatter == nil) {
            dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        }
        
        string = [dateFormatter stringFromDate:time];
        
    }
    return string;

}


+ (NSString *)getXFGDTimeString:(NSString *)timeString
{
    if (!timeString) {
        return @"";
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    if (timeString.length < 10) {
        return timeString;
    }
    
    timeString = [timeString substringWithRange:NSMakeRange(0, 10)];
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeString.doubleValue];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterFullStyle];// 修改下面提到的北京时间的日期格式
    [formatter setTimeStyle:NSDateFormatterFullStyle];// 修改下面提到的北京时间的时间格式
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"];// 此行代码与上面两行作用一样，故上面两行代码失效
    NSString *dat = [formatter stringFromDate:time];
    
    NSDate *today = [[NSDate alloc] init];
    
    NSString * todayString = [[today description] substringToIndex:10];
    NSString * thisYearString = [[today description] substringToIndex:4];
    
    NSString * dateString = [dat substringToIndex:10];
    NSString * dateYearString = [dat substringToIndex:4];
    
    NSString *string = nil;

    if ([dateString isEqualToString:todayString])
    {
//      return @"今天";
        
        static NSDateFormatter *dateFormatter = nil;
        
        if (dateFormatter == nil) {
            dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"HH:mm"];
        }
        
        string = [dateFormatter stringFromDate:time];
    }
    else if ([dateYearString isEqualToString:thisYearString])
    {
//      return @"今年";
        
        static NSDateFormatter *dateFormatter = nil;
        
        if (dateFormatter == nil) {
            dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"MM-dd"];
        }
        
        string = [dateFormatter stringFromDate:time];
    }
    else
    {
        static NSDateFormatter *dateFormatter = nil;
        
        if (dateFormatter == nil) {
            dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        }
        
        string = [dateFormatter stringFromDate:time];
    }
    
    return string;
    
}



+ (NSDate *) getDate:(NSString *)timeString
{
    if (!timeString) {
        return nil;
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    static NSDateFormatter *dateFormatter;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
    }
    return [dateFormatter dateFromString:timeString];
}

+ (NSDate *) getDate:(NSString *)timeString dateFormat:(NSString *)format
{
    if (!timeString) {
        return nil;
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    static NSDateFormatter *dateFormatter;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        
        
        if (!format) {
            
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        }else{
            
            [dateFormatter setDateFormat:format];
        }
        
    }
    return [dateFormatter dateFromString:timeString];
}


+ (NSDate *) getFullDate:(NSString *)timeString
{
    if (!timeString) {
        return nil;
    }
    
    timeString = [NSString stringWithFormat:@"%@",timeString];
    
    static NSDateFormatter *dateFormatter;
    
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    }
    return [dateFormatter dateFromString:timeString];
}

+ (NSDate *)getBeijingTime:(NSDate *)date
{
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    
    return localeDate;
}


//时间戳
+ (NSString *)getTimestamp:(NSDate *)date{
    
    NSString *timeSp = [NSString stringWithFormat:@"%ld",(long)[date timeIntervalSince1970]];
    
    return timeSp;
}

//当前时间戳
+ (NSString *)getTimestampNow{
    
    NSDate *date = [NSDate date];
    
    NSString *timeSp = [NSString stringWithFormat:@"%ld",(long)[date timeIntervalSince1970]];
    
    return timeSp;
}


@end
